<?php

/**
 * 简化数据操作类
 */
class Db
{
	public static $link=null;

	public $errorInfo;

	public $table;

	public $where='';
	public $limit='';
	public $order='';

	public $lastsql= '';
	/**
	 * [__construct 构造函数 链接数据库]
	 * @param [type] $table [description]
	 */
	public function __construct($table=null)
	{
		$this->table = $table;
		try {
			if(!self::$link){
			// // 链接数据库
				self::$link = @new PDO(config('db_type').":host=".config('db_host').";dbname=".config('db_name').";charset=".config('db_charset'),config('db_user'),config('db_pass'));
			}
		} catch (Exception $e) {
			$this->errorInfo = $e->getMessage();
		}
	}
	/**
	 * [query 查询类原生sql]
	 * @param  [type] $sql [description]
	 * @return [type]      [description]
	 */
	public function query($sql)
	{
		$this->lastsql = $sql;
		if(!self::$link){
			return false;
		}
		$res = self::$link->query($sql);

		if($res){
			return $res->fetchAll(PDO::FETCH_ASSOC);
		}else{
			$this->errorInfo = self::$link->errorInfo();
			return false;
		}
	}
	/**
	 * [exec insert delete update 原生sql]
	 * @param  [type] $sql [description]
	 * @return [type]      [description]
	 */
	public function exec($sql)
	{
		$this->lastsql = $sql;
		if(!self::$link){
			return false;
		}
		$res = self::$link->exec($sql);

		if($res){
			return $res;
		}else{
			$this->errorInfo = self::$link->errorInfo();
			return false;
		}
	}

	public function where($where)
	{
		$this->where = " where ".$where;
	}

	public function limit($limit)
	{
		$this->limit = " limit ".$limit;
	}

	public function order($order)
	{
		$this->order = " order by ".$order;
	}
	/**
	 * [select 查询 多条数据]
	 * @return [type] [description]
	 */
	public function select()
	{
		$sql = "select * from ".$this->table.$this->where.$this->order.$this->limit;

		return $this->query($sql);
	}
	/**
	 * [find 查一条数据]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public function find($id)
	{
		$key = $this->getPryKey();
		$sql = "select * from ".$this->table." where ".$key." = ".$id;

		return current($this->query($sql));
	}
	/**
	 * [getPryKey 获取主键]
	 * @return [type] [description]
	 */
	public function getPryKey()
	{
		$sql = "desc ".$this->table;

		$fields = $this->query($sql);

		foreach ($fields as $field) {
			if($field['Key']=='PRI'){
				return $field['Field'];
			}
		}
	}
	/**
	 * [save 新增]
	 * @param  [type] $data [description]
	 * @return [type]       [description]
	 */
	public function save($data)
	{
		$keys = implode(',',array_keys($data));
		$values = implode("','",$data);

		$sql = "insert into ".$this->table." (".$keys.") values('".$values."')";

		return $this->exec($sql);
	}
	/**
	 * [del 删除]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public function del($id=null)
	{
		if($id){
			$key = $this->getPryKey();

			$sql = "delete from ".$this->table." where ".$key." = ".$id;
		}else{
			$sql = "delete from ".$this->table.$this->where;
		}

		return $this->exec($sql);

	}
	/**
	 * [update 更新]
	 * @param  [type] $data [description]
	 * @return [type]       [description]
	 */
	public function update($data)
	{
		$dataStr = "";

		foreach ($data as $k => $v) {
			$dataStr .= $k."='".$v."',";
		}

		$dataStr = rtrim($dataStr,',');

		$sql = "update ".$this->table." set ".$dataStr." ".$this->where;

		return $this->exec($sql);
	}
}